package com.hqhy.like.admin.order.service.impl;

import com.hqhy.like.admin.order.dao.persist.repository.IOrderRepository;
import com.hqhy.like.admin.order.pojo.vo.OrderDetailsStandardVO;
import com.hqhy.like.admin.order.pojo.vo.OrderEcharts2VO;
import com.hqhy.like.admin.order.pojo.vo.OrderEchartsVO;
import com.hqhy.like.admin.order.pojo.vo.OrderListVO;
import com.hqhy.like.admin.order.service.IOrderService;
import com.hqhy.like.common.enumerator.ServiceCode;
import com.hqhy.like.common.ex.ServiceException;
import com.hqhy.like.common.pojo.vo.PageData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private IOrderRepository orderRepository;


    @Override
    public PageData<OrderListVO> ListOrder(Integer pageNum, Integer pageSize) {
        return orderRepository.listOrder(pageNum, pageSize);
    }

    @Override
    public List<OrderDetailsStandardVO> OrderDetailsById(Long orderId) {
        List<OrderDetailsStandardVO> order = orderRepository.getStandardById(orderId);
        if (order == null){
            String message = "查询订单详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
       // List<OrderDetailsStandardVO> orderDetailsStandardVOS = new ArrayList<>();
        return order;
    }

    @Override
    public void deleteById(Long id) {
        orderRepository.deleteById(id);
    }

    @Override
    public void deleteDetailsById(Long id) {
        orderRepository.deleteDetailsById(id);
    }

    @Override
    public List<OrderEchartsVO> getOrderByDate() {

        LocalDateTime start = LocalDate.now().plusDays(-7).atStartOfDay();
        LocalDateTime end = LocalDate.now().plusDays(1).atStartOfDay();
        return orderRepository.getOrderByDate(start,end);
    }

    @Override
    public List<OrderEcharts2VO> getEcharts() {
        LocalDateTime start = LocalDate.now().plusDays(-7).atStartOfDay();
        LocalDateTime end = LocalDate.now().plusDays(1).atStartOfDay();
        List<OrderEcharts2VO> echarts = orderRepository.getEcharts(start, end);
// 创建一个DateTimeFormatter对象，用于格式化日期
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd");
// 遍历列表，对每个OrderEcharts2VO对象进行转换
        for (OrderEcharts2VO vo : echarts) {
            LocalDateTime dateTime = vo.getGmtCreate(); // 获取LocalDateTime对象
            String formattedDate = dateTime.format(formatter); // 格式化日期
            vo.setTime(formattedDate); // 将格式化后的字符串设置回OrderEcharts2VO对象
        }

        return echarts;
    }


    /*@Override
    public List<OrderDetailsStandardVO> OrderDetailsById(Long orderId) {
        OrderDetailsStandardVO order = orderRepository.getStandardById(orderId);
        if (order == null){
            String message = "查询订单详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        List<OrderDetailsStandardVO> orderDetailsStandardVOS = new ArrayList<>();
        orderDetailsStandardVOS.add(order);
        return orderDetailsStandardVOS;
    }
*/
    @Override
    public List<OrderListVO> OrderByOrderNo(String orderNo) {
        OrderListVO standardByOrderNo = orderRepository.getStandardByOrderNo(orderNo);
        if (standardByOrderNo == null){
            String message = "查询订单失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        List<OrderListVO> orderListVOS = new ArrayList<>();
        orderListVOS.add(standardByOrderNo);
        return orderListVOS;
    }

    @Override
    public PageData<OrderDetailsStandardVO> ListOrderDetails(Integer pageNum, Integer pageSize) {

        return orderRepository.listOrderDetails(pageNum, pageSize);
    }


}
